<!--

-->

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <cpn
      :number1='num1'
      :number2='num2'
      @ccnumb1='changenumb1'
      @ccnumb2='changenumb2'
    ></cpn>
    <!-- <cpn
      :number1='num1'
      :number2='num2'
      :ccnumb1='val => {num1 = val}'
      :ccnumb2='val => {num2 = val}'
    ></cpn> -->
    <div>
      <h4>num1={{num1}}</h4>
      <h4>num2={{num2}}</h4>
    </div>
  </div>


  <!-- 模版1 -->
  <template id='cpn'>
    <div>
      <h2>props:{{number1}}</h2>
      <input type="text"
        :value='number1'
        @input='changeNumb1($event)'
      >
      <h2>props:{{number2}}</h2>
      <input
        type="text"
        :value='number2'
        @input='changeNumb2($event)'
      >
    </div>
  </template>


  <!-- 模版2 -->
  <template id='cpn222'>
    <div>
      <h2>props:{{number1}}</h2>
      <h2>data:{{dnumber1}}</h2>
      <input type="text"
        :value='dnumber1'
        @input='changeNumb1($event)'
      >
      <h2>props:{{number2}}</h2>
      <h2>data:{{dnumber2}}</h2>
      <input
        type="text"
        :value='dnumber2'
        @input='changeNumb2($event)'
      >
    </div>
  </template>

  <script src="../../js/vue.js"></script>
  <script>
    const cpn = {
      template: '#cpn',    //模版1
      // template: '#cpn222', //模版2
      props: {
        number1: Number,
        number2: Number,
      },
      data() {
        return { //仅用与展示，不能在当前组件内部改变
          dnumber1: this.number1,
          dnumber2: this.number2,
        }
      },
      methods: {
        changeNumb1(e) {
          let value = e.target.value;
          // 第一种 string number
          let num = Number(value);
          // 第二种 string number
          // let num = parseInt(value);
          console.log('changeNumb1=', num)
          this.$emit('ccnumb1', num)

          // 模版2
          // this.dnumber1 = num;
        },
        changeNumb2(e) {
          let value = e.target.value;
          let num = Number(value);
          console.log('changeNumb222222', num)
          this.$emit('ccnumb2', num)

          // 模版2
          // this.dnumber1 = num;
        }
      }
    }

    const app = new Vue({
      el: '#app',
      data: {
        num1: 1,
        num2: 0,
      },
      components: {
        cpn,
      },
      methods: {
        changenumb1(num) {
          console.log('root 1')
          this.num1 = num;
        },
        changenumb2(num) {
          console.log('root 2')
          this.num2 = num;
        }
      }
    })
  </script>
</body>
</html>